package com.bookshop.service.impl;

import com.bookshop.entity.*;
import com.bookshop.entity.request.SearchForm;
import com.bookshop.mapper.BookMapper;
import com.bookshop.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: BookServiceImp
 * Package: com.bookshop.service.impl
 * Description:
 *
 * @Author BJX
 * @Create 2024/1/18 9:35
 * @Version 1.0
 */
@Service
public class BookServiceImp implements BookService {
    private BookMapper bookMapper;
    @Autowired
    public BookServiceImp(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    // 查询图书详情
    @Override
    public Book getBookById(long id) {
        return bookMapper.selectBookById(id);
    }


    //搜索页
    @Override
    public BookList searchBook(SearchForm s){

        PageHelper.startPage(s.getPageNum(), s.getPageSize());
        PageHelper.orderBy(s.getOrderField() + " " + s.getOrderStyle());//指定排序字段和排序方式

        List<Book> bookList = bookMapper.selectBookByKey(s);
        PageInfo<Book> pageInfo = new PageInfo<>(bookList);

        return new BookList(pageInfo.getList(),pageInfo.getTotal(),pageInfo.getPages());
    }

//    @Override
//    public HashMap<String, List<Book>> homeClassify(int classifyId) {
//        HashMap<String, List<Book>> hashMap=new HashMap<>();
//
//        List<SecondaryCategory> categoryList=bookMapper.selectSecondaryId(classifyId);
//        Iterator<SecondaryCategory> iterator = categoryList.iterator();
//        while(iterator.hasNext()){
//            SecondaryCategory value = iterator.next();
//            List<Book> bookList=bookMapper.selectBookBySecondary(value.getId());
//            hashMap.put(value.getCategoryName(),bookList);
//        }
//
//        return hashMap;
//    }

    @Override
    public List<List<List<Book>>> homeClassify() {
        List<List<List<Book>>> bookList = new ArrayList<>();
        List<Integer> classifyList = bookMapper.getAllFirstClassify();

        for (int i = 0; i < classifyList.size(); i++) {
            int classifyFirst = classifyList.get(i);
            List<List<Book>> bookTempList1 = new ArrayList<>();
            List<Integer> classifySecondList=bookMapper.selectSecondaryId(classifyFirst);

            for(int j = 0; j < classifySecondList.size(); j++){
                int classifySecond = classifySecondList.get(j);
                List<Book> bookTempList2=bookMapper.selectBookBySecondary(classifySecond);
                bookTempList1.add(bookTempList2);
            }
            bookList.add(bookTempList1);
        }
        return bookList;
    }

    @Override
    public List<ClassifyList> getClassify() {
        List<ClassifyList> classifyLists = bookMapper.getClassify();
        for (int i = 0; i < classifyLists.size(); i++) {
            int classifyFirst = classifyLists.get(i).getFirstId();
            List<String> classifySecondList=bookMapper.selectSecondaryName(classifyFirst);
            classifyLists.get(i).setSecondList(classifySecondList);
        }
        return classifyLists;
    }

    @Override
    public List<List<SimpleClassify>> getSimpleClassify() {

        List<List<SimpleClassify>> classifyLists = new ArrayList<>();

        List<SimpleClassify> classifyList1 = bookMapper.getSimpleClassify();
        classifyList1.add(0, new SimpleClassify(0, "全部"));

        List<SimpleClassify> classifyList2 = new ArrayList<>();
        classifyList2.add(new SimpleClassify(0, "全部"));
        classifyList2.add(new SimpleClassify(1, "0-50"));
        classifyList2.add(new SimpleClassify(2, "50-150"));
        classifyList2.add(new SimpleClassify(3, "150-500"));
        classifyList2.add(new SimpleClassify(4, "500以上"));

        List<SimpleClassify> classifyList3 = bookMapper.getPressClassify();
        classifyList3.add(0, new SimpleClassify(0, "全部"));

        classifyLists.add(classifyList1);
        classifyLists.add(classifyList2);
        classifyLists.add(classifyList3);

        return classifyLists;
    }

}
